import store from "@/store";
import axios from "axios";

//接口访问域名
// export const host = "http://47.104.142.82:20520";
// export const host = "http://172.31.1.158:20520";
// export const host = "http://10.0.0.110:20520";
// export const host = "http://116.178.68.235:12313";
// export const host = "http://127.0.0.1:20520";
export const host = "http://47.104.142.82:20520";
// export const host = "http://43.138.61.155:20520";
// export const host = "http://10.0.0.36:20520";
// export const host = "http://10.0.0.68:20520";
// export const host = "http://10.70.13.10:20520";

//WebSocket地址
// export const webSocketUrl = "ws://47.104.142.82:20521"
// export const webSocketUrl = "ws://172.31.1.158:20521";
// export const webSocketUrl = "ws://10.0.0.110:20521"
// export const webSocketUrl = "ws://116.178.68.235:62334"
// export const webSocketUrl = "ws://127.0.0.1:20521"
export const webSocketUrl = "ws://47.104.142.82:20521";
// export const webSocketUrl = "ws://43.138.61.155:20521";
// export const webSocketUrl = "ws://10.0.0.36:20521";
// export const webSocketUrl = "ws://10.0.0.68:20521";
// export const webSocketUrl = "ws://10.70.13.10:20521";

export const resourceTypeMap = {
    1: '其他',
    2: '客户端安装文件',
    3: '管线PB',
    4: '遥测PB',
    5: '险情图片',
    6: '险情处理图片',
    7: '桩牌图片',
    8: '设施图片',
    9: '施工作业区图片',
    10: '用户头像图片',
    11: '客户端logo图片'
}

//接口访问路径
export const url = {
    danger_type_modify : "/api/2.0/dangerType/admin/modify",
    danger_type_delete2 : "/api/2.0/dangerType/admin/delete/logical/",
    danger_type_list : "/api/2.0/dangerType/list",
    danger_type_delete : "/api/2.0/dangerType/admin/delete/",
    danger_type_insert : "/api/2.0/dangerType/admin/insert",
    pile_card_list : "/api/2.0/pileCard/list",
    pile_card : "/api/2.0/pileCard/",
    pile_card_modify : "/api/2.0/pileCard/modify",
    pile_card_delete2 : "/api/2.0/pileCard/delete/logical/",
    pile_card_delete : "/api/2.0/pileCard/delete/",
    pile_card_insert : "/api/2.0/pileCard/insert",
    pile_card_load : "/api/2.0/pileCard/load",
    pile_card_upload : "/api/2.0/pileCard/upload",
    pipeline_list : "/api/2.0/pipeline/list",
    pipeline : "/api/2.0/pipeline/",
    pipeline_modify : "/api/2.0/pipeline/admin/modify",
    pipeline_delete2 : "/api/2.0/pipeline/admin/delete/logical/",
    pipeline_delete : "/api/2.0/pipeline/admin/delete/",
    pipeline_list_oss : "/api/2.0/pipeline/list/oss/details",
    pipeline_list_db : "/api/2.0/pipeline/list/details",
    pipeline_electronic_fence : "/api/2.0/pipeline/electronicFence/",
    pipeline_details : "/api/2.0/pipeline/details/",
    pipeline_upload : "/api/2.0/pipeline/admin/upload",
    pipeline_upload2 : "/api/2.0/pipeline/admin/upload2",
    pipeline_load : "/api/2.0/pipeline/admin/load",
    general_facilities_type_modify : "/api/2.0/generalFacilitiesType/admin/modify",
    general_facilities_type_delete2 : "/api/2.0/generalFacilitiesType/admin/delete/logical/",
    general_facilities_type_list : "/api/2.0/generalFacilitiesType/list",
    general_facilities_type_delete : "/api/2.0/generalFacilitiesType/admin/delete/",
    general_facilities_type_insert : "/api/2.0/generalFacilitiesType/admin/insert",
    pipeline_station_list : "/api/2.0/pipelineStation/list",
    pipeline_station : "/api/2.0/pipelineStation/",
    pipeline_station_modify : "/api/2.0/pipelineStation/admin/modify",
    pipeline_station_delete2 : "/api/2.0/pipelineStation/admin/delete/logical/",
    pipeline_station_delete : "/api/2.0/pipelineStation/admin/delete/",
    pipeline_station_insert : "/api/2.0/pipelineStation/admin/insert",
    file_list : "/api/2.0/file/admin/list",
    file : "/api/2.0/file/admin/",
    file_delete2 : "/api/2.0/file/admin/delete/logical/",
    file_analysis : "/api/2.0/file/admin/analysis",
    file_clean : "/api/2.0/file/admin/clean",
    file_upload : "/api/2.0/file/admin/upload",
    file_uploads : "/api/2.0/file/admin/uploads",
    file_download : "/api/2.0/file/admin/download",
    file_delete : "/api/2.0/file/admin/delete/",
    danger_list : "/api/2.0/danger/list",
    danger : "/api/2.0/danger/",
    danger_modify : "/api/2.0/danger/modify",
    danger_delete2 : "/api/2.0/danger/delete/logical/",
    danger_details : "/api/2.0/danger/details/",
    danger_delete : "/api/2.0/danger/admin/delete/",
    danger_insert : "/api/2.0/danger/insert",
    danger_statistics : "/api/2.0/danger/statistics",
    construction_units_list : "/api/2.0/constructionUnits/list",
    construction_units : "/api/2.0/constructionUnits/",
    construction_units_modify : "/api/2.0/constructionUnits/modify",
    construction_units_delete2 : "/api/2.0/constructionUnits/delete/logical/",
    construction_units_delete : "/api/2.0/constructionUnits/delete/",
    construction_units_insert : "/api/2.0/constructionUnits/insert",
    camera_list : "/api/2.0/camera/list",
    camera : "/api/2.0/camera/",
    camera_modify : "/api/2.0/camera/modify",
    camera_delete2 : "/api/2.0/camera/delete/logical/",
    camera_delete : "/api/2.0/camera/delete/",
    camera_insert : "/api/2.0/camera/insert",
    camera_type_list : "/api/2.0/cameraType/list",
    camera_type : "/api/2.0/cameraType/",
    camera_type_modify : "/api/2.0/cameraType/admin/modify",
    camera_type_delete2 : "/api/2.0/cameraType/admin/delete/logical/",
    camera_type_delete : "/api/2.0/cameraType/admin/delete/",
    camera_type_insert : "/api/2.0/cameraType/admin/insert",
    client_software_list : "/api/2.0/clientSoftware/admin/list",
    client_software_version : "/api/2.0/clientSoftware/version/",
    client_software_latest_version : "/api/2.0/clientSoftware/latestVersion",
    client_software_is_latest_version : "/api/2.0/clientSoftware/isLatestVersion",
    client_software_modify : "/api/2.0/clientSoftware/admin/modify",
    client_software_delete2 : "/api/2.0/clientSoftware/admin/delete/logical/",
    client_software_delete : "/api/2.0/clientSoftware/admin/delete/",
    client_software_insert : "/api/2.0/clientSoftware/admin/insert",
    pile_card_type_list : "/api/2.0/pileCardType/list",
    pile_card_type_modify : "/api/2.0/pileCardType/admin/modify",
    pile_card_type_delete2 : "/api/2.0/pileCardType/admin/delete/logical/",
    pile_card_type_delete : "/api/2.0/pileCardType/admin/delete/",
    pile_card_type_insert : "/api/2.0/pileCardType/admin/insert",
    project_list : "/api/2.0/project/admin/list",
    project_cp_box_list : "/api/2.0/project/admin/cpBoxList/",
    project : "/api/2.0/project/admin/",
    project_modify : "/api/2.0/project/admin/modify",
    project_delete2 : "/api/2.0/project/admin/delete/logical/",
    project_delete : "/api/2.0/project/admin/delete/",
    project_insert : "/api/2.0/project/admin/insert",
    project_type_select : "/api/2.0/project/admin/typeSelect",
    project_validate_cpa : "/api/2.0/project/admin/validateCpAccount",
    project_cpa_access : "/api/2.0/project/admin/cloudPlatformAccount/access",
    project_cpa_start : "/api/2.0/project/admin/cloudPlatformAccount/start",
    project_cpa_shutdown : "/api/2.0/project/admin/cloudPlatformAccount/shutdown",
    project_validate_yka : "/api/2.0/project/admin/validateYkAccount",
    project_yka_access : "/api/2.0/project/admin/yuKongAccount/access",
    project_yka_start : "/api/2.0/project/admin/yuKongAccount/start",
    project_yka_shutdown : "/api/2.0/project/admin/yuKongAccount/shutdown",
    general_facilities_list : "/api/2.0/generalFacilities/list",
    general_facilities : "/api/2.0/generalFacilities/",
    general_facilities_modify : "/api/2.0/generalFacilities/modify",
    general_facilities_load : "/api/2.0/generalFacilities/load",
    general_facilities_delete2 : "/api/2.0/generalFacilities/delete/logical/",
    general_facilities_delete : "/api/2.0/generalFacilities/delete/",
    general_facilities_insert : "/api/2.0/generalFacilities/insert",
    role_list : "/api/2.0/role/admin/list",
    role : "/api/2.0/role/admin/",
    role_modify : "/api/2.0/role/admin/modify",
    role_delete2 : "/api/2.0/role/admin/delete/logical/",
    role_delete : "/api/2.0/role/admin/delete/",
    role_ : "/api/2.0/role/admin/insert",
    orthographic_image : "/api/2.0/orthographicImage/",
    orthographic_image_modify : "/api/2.0/orthographicImage/admin/modify",
    orthographic_image_delete2 : "/api/2.0/orthographicImage/admin/delete/logical/",
    orthographic_image_delete : "/api/2.0/orthographicImage/admin/delete/",
    orthographic_image_list : "/api/2.0/orthographicImage/list",
    orthographic_image_group_list : "/api/2.0/orthographicImage/group/list",
    orthographic_image_insert : "/api/2.0/orthographicImage/admin/insert",
    construction_type_modify : "/api/2.0/constructionType/admin/modify",
    construction_type_delete2 : "/api/2.0/constructionType/admin/delete/logical/",
    construction_type_list : "/api/2.0/constructionType/list",
    construction_type_delete : "/api/2.0/constructionType/admin/delete/",
    construction_type_insert : "/api/2.0/constructionType/admin/insert",
    high_consequence_areas_list : "/api/2.0/highConsequenceAreas/list",
    high_consequence_areas : "/api/2.0/highConsequenceAreas/",
    high_consequence_areas_modify : "/api/2.0/highConsequenceAreas/modify",
    high_consequence_areas_delete2 : "/api/2.0/highConsequenceAreas/delete/logical/",
    high_consequence_areas_delete : "/api/2.0/highConsequenceAreas/delete/",
    high_consequence_areas_insert : "/api/2.0/highConsequenceAreas/insert",
    high_consequence_areas_type_modify : "/api/2.0/highConsequenceAreasType/admin/modify",
    high_consequence_areas_type_delete2 : "/api/2.0/highConsequenceAreasType/admin/delete/logical/",
    high_consequence_areas_type_list : "/api/2.0/highConsequenceAreasType/list",
    high_consequence_areas_type_delete : "/api/2.0/highConsequenceAreasType/admin/delete/",
    high_consequence_areas_type_insert : "/api/2.0/highConsequenceAreasType/admin/insert",
    fly_task_list : "/api/2.0/flyTask/list",
    fly_task : "/api/2.0/flyTask/",
    fly_task_modify : "/api/2.0/flyTask/modify",
    fly_task_delete2 : "/api/2.0/flyTask/delete/logical/",
    fly_task_statistics : "/api/2.0/flyTask/flight/statistics",
    fly_task_cp_liveVideo : "/api/2.0/flyTask/cp/liveVideo/",
    fly_task_report : "/api/2.0/flyTask/report/",
    fly_task_platform : "/api/2.0/flyTask/platformTask",
    fly_task_delete : "/api/2.0/flyTask/admin/delete/",
    fly_task_insert : "/api/2.0/flyTask/admin/insert",
    statistics_summary : "/api/2.0/statistics/admin/projectSummary",
    statistics_user : "/api/2.0/statistics/admin/user",
    statistics_file : "/api/2.0/statistics/admin/file",
    statistics_pipeline : "/api/2.0/statistics/admin/pipeline",
    statistics_pipeline_station : "/api/2.0/statistics/admin/pipelineStation",
    statistics_fly_task : "/api/2.0/statistics/admin/flyTask",
    statistics_infrastructure : "/api/2.0/statistics/admin/infrastructure",
    statistics_infrastructure_type : "/api/2.0/statistics/admin/infrastructureType",
    statistics_danger : "/api/2.0/statistics/admin/danger",
    user : "/api/2.0/user/",
    user_modify : "/api/2.0/user/modify",
    user_delete2 : "/api/2.0/user/delete/logical/",
    user_logout : "/api/2.0/user/logout",
    user_inspectors : "/api/2.0/user/inspectors",
    user_list : "/api/2.0/user/list",
    user_delete : "/api/2.0/user/admin/delete/",
    user_register : "/api/2.0/user/register",
    user_login : "/api/2.0/user/login",
    user_state : "/api/2.0/user/state",
    danger_process_list : "/api/2.0/dangerProcess/list",
    danger_process : "/api/2.0/dangerProcess/admin/",
    danger_process_modify : "/api/2.0/dangerProcess/modify",
    danger_process_delete2 : "/api/2.0/dangerProcess/delete/logical/",
    danger_process_delete : "/api/2.0/dangerProcess/admin/delete/",
    danger_process_insert : "/api/2.0/dangerProcess/insert",
    danger_process_list_danger : "/api/2.0/dangerProcess/list/danger/",
}

//地图瓦片加载地址
export const mapUrl = {
    ArcGIS: "http://services.arcgisonline.com/ArcGIS/services/World_Imagery/MapServer?mapname=Layers&layer=_alllayers&level={z}&row={y}&column={x}&format=PNG", // arcgis
    TianYu: "http://gis.tianyujingwei.com:9099/satellitemap/Chinanooffset2/Chinanooffset3/瓦片_谷歌/{z}/{x}/{y}.jpg", // 天宇地图
    GaoDe:  "https://wprd01.is.autonavi.com/appmaptile?style=6&lang=zh_cn&scl=2&x={x}&y={y}&z={z}", // 高德地图
}

//配置路由
export function routerConfig(router){
    router.beforeEach((to, from, next) => {
        /* 路由发生变化修改页面title */
        if (to.meta.title) {
            document.title = to.meta.title
        }
        next();
    });
}

//配置axios拦截器
export function axiosConfig($Message,axios){
    axios.defaults.baseURL = host;
    axios.defaults.headers.common['u-token'] = "";
    axios.defaults.headers.common['u-uid'] = "";
    axios.defaults.headers.common['u-projectId'] = "";
    axios.defaults.timeout = 1000 * 60 * 60;
    axios.defaults.headers.get['Content-Type'] = 'application/json';
    axios.defaults.headers.post['Content-Type'] = 'application/json';
    axios.interceptors.request.use(config => {
            if (config.url === store.state.url.user_login) {
                return config;
            }
            if (config.url.startsWith(store.state.resourceHost)){
                return config;
            }
            let axios_token = config.headers.common["u-token"];
            let axios_uid = config.headers.common["u-uid"];
            let axios_project_id = config.headers.common["u-projectId"];
            if (axios_token === "" || axios_uid === "" || axios_project_id === "") {
                let token = window.localStorage.getItem("u-token");
                let uid = window.localStorage.getItem("u-uid");
                let project_id = window.localStorage.getItem("u-projectId");
                if (token == null || uid == null || project_id == null) {
                    if (config.url === url.user_state){
                        return config;
                    }
                    throw Error("未登录...");
                } else {
                    config.headers.common["u-token"] = token;
                    config.headers.common["u-uid"] = uid;
                    config.headers.common["u-projectId"] = project_id;
                }
            }
            return config;
        }, error => Promise.reject(error)
    );
    axios.interceptors.response.use(response => {
        if (response.data.state === 200) {
            return response;
        } else {
            if (response.config.url === url.user_state){
                return response;
            }
            if (response.config.url.startsWith(store.state.resourceHost)){
                return response
            }
            if (response.data.state === -201){
                store.state.isLogin = false;
                return response;
            }
            if (response.data.state === -200){
                return response;
            }
            if (response['data']['type'] === 'application/pdf'){
                return response;
            }
            $Message.error(response.data.message);
            throw Error(response.data.message);
        }
    }, error => {
        return Promise.reject(error);
    });
}

export class Request {
    /**
     * 获取项目信息
     * @return {Promise<AxiosResponse<any>>}
     */
    static selectProjectList() {
        return axios.get(url.project_list, {params: {pageSize:-1,descParams:''}})
    }

    /**
     * 项目名称渲染
     * @param createElement
     * @param data
     * @return {*}
     */
    static projectNameRender (createElement,data){
        let {projectId} = data.row;
        return createElement('span',store.state.projectMap.get(projectId));
    }
}
